 
import Foundation
import Flutter
import UIKit
import ContactsUI
public class DeviceDataPlugin {
    
    var mMessageChannel:FlutterMethodChannel
    var mFlutterCV:FlutterViewController
    
    
    init(controller :FlutterViewController){
        mFlutterCV=controller
        mMessageChannel = FlutterMethodChannel(name: "DeviceDataPlugin", binaryMessenger: controller.binaryMessenger)
        mMessageChannel.setMethodCallHandler(self.handle)
    }
    
    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        
        if call.method == "getContact" {
            self.getContact(call, result: result)
        }else if call.method == "getRequestDefaultParams" {
            self.getRequestDefaultParams(call, result: result)
        }else if call.method == "getDeviceInfo" {
            self.getDeviceInfo(call, result: result)
        }else if call.method == "getContacts" {
            self.getContacts(call, result: result)
        }else if call.method == "getIdfv" {
            result(LDeviceTool.idfv())
        }else if call.method == "getIdfa" {
            result(LDeviceTool.info.idfa())
        }else if call.method == "isVPN" {
            result(LDeviceTool.isVPN ? "1":"0")
        } else {
            result(FlutterMethodNotImplemented)
        }
    }
    

    public func getRequestDefaultParams(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String?
        var dict = [String : String]()
        dict["deviceName"]=LDeviceTool.modelName
        dict["idfa"]=LDeviceTool.info.idfa()
        dict["appVersion"]=appVersion ?? ""
        dict["deviceId"]=LDeviceTool.idfv()
        dict["deviceOsVersion"]=LDeviceTool.iOSVersion
        result(dict)
    }

    public func getContact(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
//        let contactPicker = CNContactPickerViewController()
//              contactPicker.delegate = self
//              present(contactPicker, animated: true, completion: nil)

    }
    public func getContacts(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
 
        
    }
    func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
         // 处理已经选中的联系人信息
     }

     func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
         // 取消选择操作时的处理代码
     }
    public func getDeviceInfo(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
       
        let jsonString=convertDictionaryToString(data:  LDeviceTool.getDeviceInfo())
        result(jsonString)
        //
    }
    
    func convertDictionaryToString(data:Any) -> String {
        var jsonStr = ""
        if let dictionary = data as? [String: Any] {
            if let jsonData = try? JSONSerialization.data(withJSONObject: dictionary, options: []) {
                jsonStr = String(data: jsonData, encoding: .utf8) ?? ""
            }
        }
        if let array = data as? [Any] {
            if let jsonData = try? JSONSerialization.data(withJSONObject: array, options: []) {
                jsonStr = String(data: jsonData, encoding: .utf8) ?? ""
            }
        }
        return jsonStr
    }
}

